package optional;

import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * 类名：MainTest
 * 开发人员: CoderJu
 * 创建时间: 2019/3/9 17:58
 * 描述:
 * 版本：V1.0
 */
public class MainTest {

    public static void main(String[] args) {
        //createOptional();
        //isPresent();
        //equals();
        //get();
        //hashCodeTest();
        //orElse();
        //orElseGet();
        //ifPresent();
        //filter();
        //map();
        //flatMap();
        //toStringTest();.
        orElseThrow();

    }




    public static void createOptional(){
        //创建空的Optional对象
        Optional optional = Optional.empty();
        System.out.println("optional >>>"+optional.isPresent());
        System.out.println("---------------------");

        //调用of方法创建 of(obj),如果obj为null,会抛出异常
        Optional optional1 = Optional.of("张三");
        System.out.println("optional1 >>>"+optional1);
        System.out.println("optional >>>"+optional1.isPresent());
        System.out.println("---------------------");
        //Optional optional2 = Optional.of(null);

        //如果为非空，返回 Optional 描述的指定值，否则返回空的 Optional。这种情况下不会抛出NPL
        Optional optional2 = Optional.ofNullable(null);
        System.out.println("optional3 >>>"+optional2.isPresent());
        Optional optional3 = Optional.ofNullable("呜呜呜呜呜");
        System.out.println("optional3 >>>"+optional3);
        System.out.println("optional3 >>>"+optional3.isPresent());
    }

    public static void isPresent(){
        Optional optional = Optional.of("A");
        System.out.println("optional >>>"+ optional.isPresent());
        optional = Optional.empty();
        System.out.println("optional empty >>>"+ optional.isPresent());
    }

    public static void equals(){
        Optional optional = Optional.of("A");
        String str = "A";
        Optional optional1 = Optional.of("A");
        Optional optional2 = Optional.of("B");
        System.out.println("optional >>>"+ optional.equals("A"));
        System.out.println("optional String>>>"+ optional.equals(str));
        System.out.println("optional Optional>>>"+ optional.equals(optional1));
        System.out.println("optional Optional>>>"+ optional.equals(optional2));
    }

    public static void get(){
        Optional optional = Optional.of("AbcdEf");
        String s = (String) optional.get();
        System.out.println("get >>>"+ s);
        Optional optional1 = Optional.ofNullable(null);
        optional1.get();
    }

    public static void  hashCodeTest(){
        Optional optional = Optional.of("AbcdEf");
        int s = optional.hashCode();
        System.out.println("hashCodeTest >>>"+ s);
        optional = Optional.of("A");
        s = optional.hashCode();
        System.out.println("hashCodeTest >>>"+ s);
        optional = Optional.ofNullable(null);
        s = optional.hashCode();
        System.out.println("hashCodeTest >>>"+ s);
    }

    public static void orElse(){
        Optional optional = Optional.of("AbcdEf");
        System.out.println("orElse >>>"+  optional.orElse("A"));
        System.out.println("orElse >>>"+  optional.orElse("K"));
        optional = Optional.empty();
        System.out.println("orElse >>>"+  optional.orElse("Null"));
    }

    public static void orElseGet(){
        Optional optional = Optional.of("AbcdEf");
        System.out.println("orElse >>>"+  optional.orElseGet(()->"111"));
        optional = Optional.empty();
        System.out.println("orElse >>>"+  optional.orElseGet(()->"111"));
    }

    private static void ifPresent() {
        Optional optional = Optional.of("AbcdEf");
        optional.ifPresent(x-> System.out.println(x+"JACK"));
        optional = Optional.empty();
        optional.ifPresent(x-> System.out.println(x+"JACK"));
    }

    private static void filter(){
        Optional<String> optional = Optional.of("AbcdEf");
        optional = optional.filter(x->x.indexOf("c")>0);
        System.out.println("optional >>>"+optional);
        optional = optional.filter(x->x.indexOf("c")<0);
        System.out.println("optional >>>"+optional);
    }

    private static void map(){
        Optional<String> optional = Optional.of("AbcdEf");
        optional.map(x->x+"1111");
        System.out.println(">>>>>>"+ optional.map(x->x+"1111"));
        optional = Optional.empty();
        optional.map(x->x+"1111");
        System.out.println(">>>>>>"+ optional.map(x->x+"1111"));
    }

    private static void flatMap() {
        Optional<String> optional = Optional.of("AbcdEf");
        optional = optional.flatMap(x->{ return  Optional.ofNullable(x+"8888888888");});
        System.out.println(">>>>>>" + optional);
        optional = Optional.empty();
        optional = optional.flatMap(x->{ return  Optional.ofNullable(x+"8888888888");});
        System.out.println(">>>>>>"+ optional);
    }
    private static void toStringTest() {
        Optional<String> optional = Optional.of("AbcdEf");
        String str = optional.toString();
        System.out.println(">>>>>>"+ str);
        optional = Optional.empty();
        str = optional.toString();
        System.out.println(">>>>>>"+ str);
    }

    private static void  orElseThrow(){
        Optional<String> optional = Optional.of("AbcdEf");
        try {
            optional.orElseThrow(IllegalAccessException::new);
            System.out.println("optional>>>>"+optional);
        }catch (Throwable e){
            System.out.println("error>>>>"+e.getMessage());
        }
        optional = Optional.empty();
        try {
            optional.orElseThrow(IllegalAccessException::new);
            System.out.println(optional);
        }catch (Throwable e){
            System.out.println("error>>>>"+e.getMessage());
        }
    }


}
